package xin.nick.cloud.code.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import xin.nick.cloud.code.dao.AuthCodeDao;
import xin.nick.cloud.pojo.AuthCode;
import xin.nick.cloud.service.CodeService;

import java.util.Objects;
import java.util.Optional;

@Service
@Slf4j
public class CodeServiceImpl implements CodeService {

    @Autowired
    private AuthCodeDao authCodeDao;

    @Override
    public int validate(String email, String code) {
        log.info("让我看看验证码对不对~ {}:{}", email, code);
        AuthCode queryAuthCode = new AuthCode();
        queryAuthCode.setEmail(email);
        queryAuthCode.setCode(code);
        Example example = Example.of(queryAuthCode);
        Optional one = authCodeDao.findOne(example);
        if (one.isEmpty() || Objects.isNull(one.get())) {
            return 0;
        }
        return 1;
    }
}
